"""Some utility functions."""

__author__  =  'Tailgunner'
__project__ =  'MW:O Drop Planner'

class WebTools():
    """Some utility functions for the web."""

    @classmethod
    def uniquify(cls, seq): 
        seen = {}
        result = []
   
        for item in seq:
            if not item in seen:
                seen[item] = 1
                result.append(item)
                
        return result


    @classmethod
    def get_array(cls, request, name, default_value="", unique=False, sort=False ):
    
        string = request.get(name, default_value=default_value)
        
        array = []
        if string == "":
            array = []
        else:
            array = [i.strip() for i in string.split(",") if i != ""]
            if len(array) and array[0] == "":
                array = [i]
                
    
        if unique:
            array = cls.uniquify(array)
        
        if sort:
            array = sorted(array)
        
        return array
    
    @classmethod
    def utf8_ify(cls, text):
        """Replace some problematic characters which may mess up html
           with their non-messy counterparts."""
        
        text = text.replace("-", u"\u208B")
        text = text.replace("=", u"\uFF1D")
        text = text.replace("'", u"\u2019")
        text = text.replace('"', u"\u201D")
        text = text.replace(" ", u"\u2000")
        text = text.replace(">", u"\uFF1E")
        text = text.replace("<", u"\uFF1C")
        text = text.replace("&", u"\uFF06")
        text = text.replace(",", u"\u201A")
        
        return text
    
    @classmethod
    def de_utf8_ify(cls, text):
        """Reverses utf8_ify."""
        
        text = text.replace(u"\u208B", "-")
        text = text.replace(u"\uFF1D", "=")
        text = text.replace(u"\u2019", "'")
        text = text.replace(u"\u201D", '"')
        text = text.replace(u"\u2000", " ")
        text = text.replace(u"\uFF1E", ">")
        text = text.replace(u"\uFF1C", "<")
        text = text.replace(u"\uFF06", "&")
        text = text.replace(u"\u201A", ",")
        
        return text